package com.yulp.springboot2task.web;

import com.yulp.springboot2task.service.TestJesQueImpl;
import com.yulp.springboot2task.service.TestServiceImpl;
import com.yulp.springboot2task.test.TestAction;
import net.greghaines.jesque.Job;
import net.greghaines.jesque.worker.Worker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @date: 2019/11/4 16:34.
 * @author: yulipeng.
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private TestServiceImpl testServiceImpl;

    @Autowired
    private TestJesQueImpl testJesQueImpl;


    @GetMapping("/a4")
    public String a4() {
        testJesQueImpl.a1("kaishi1111", "param===22", 0);
        System.out.println("============");
        return "SUCCESS";
    }
    //=====================================

    @GetMapping("/a3")
    public String a3() {
        Map map = new HashMap();
        testServiceImpl.quartz4();
        System.out.println("============");
        return "SUCCESS";
    }


    @GetMapping("/a2")
    public String a2(String jobType, String orderNo) {
        testServiceImpl.quartzJobTest(orderNo, jobType);
        System.out.println("============");
        return "SUCCESS";
    }

    /**
     * jesque
     * @return
     */
    @GetMapping("/a1")
    public String a1() {

        String queName = "quq1";
        //String className = "TestAction";
        Class<?> zlass = TestAction.class;

        Class<?> zlass2 = TestAction.class;
        Job job1 = new Job(zlass2.getName(), new Object[]{1, 2.3, true, "test", Arrays.asList("inner", 4.5)});
        testServiceImpl.executeJob(queName, job1, 1, 2,  TestAction.class);


        //testServiceImpl.test2();
        final Job job = new Job(zlass.getName(), new Object[]{1, 2.3, true, "test", Arrays.asList("inner", 4.5)});
        Worker worker = testServiceImpl.executeJob(queName, job, 1, 2, TestAction.class);

        // 启动监听者
        //Worker worker = testServiceImpl.worker(queName, className, TestAction.class);

        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 取消任务； 建议在finally中执行；
        testServiceImpl.removeRecurringEnqueue(queName, job, worker);

        System.out.println("============");
        return "SUCCESS";
    }
}
